<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Privilege extends Controller_Admin_Main
{

    private $privilege;

    public function before()
    {
        parent::before();
        $this->privilege = Model::factory('user_privilege');
        I18n::set_current_file('privilege');
    }

    public function action_add()
    {
        $id = intval($this->request->param('id'));
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['module'] = Arr::get($_POST, 'module', '');
            $data['parent_id'] = Arr::get($_POST, 'parent_id', '');
            $data['controller'] = Arr::get($_POST, 'controller', '');
            $data['action'] = Arr::get($_POST, 'action', '');
            $data['data'] = Arr::get($_POST, 'data', '');
            $data['display'] = Arr::get($_POST, 'display', '');
            $data['is_url'] = Arr::get($_POST, 'is_url', '');
            $data['url'] = Arr::get($_POST, 'url', '');
            $data['position'] = intval(Arr::get($_POST, 'position', 0));

            if (!$this->privilege->validate($data))
            {
                Message::error(__('privilege validate error'));
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'add')));
            }

            if ($this->privilege->is_exist(array('name' => $data['name'])))
            {
                Message::error(__('privilege name exist'));
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'add')));
            }

            if ($this->privilege->insert($data))
            {
                Message::success(__('privilege add success'));
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'add')));
            }
        }
        //获取一级的权限
        $privilege_option = $this->privilege->get_privilege_list(array('parent_id' => $id));
        $this->template->assign('privilege_option', $privilege_option);
        $this->template->view('user_privilege_add');
    }

    public function action_list()
    {
        $id = intval($this->request->param('id'));
        $privilege_list_temp = $this->privilege->get_privilege_list(array('parent_id' => $id));
        $privilege_list = array();
        if (Arr::is_array($privilege_list_temp))
        {
            foreach ($privilege_list_temp as $key => $value)
            {
                $parent = $this->privilege->get_row(array('id' => $value['parent_id']), 'name,parent_id');
                $parent = count($parent) > 0 ? $parent : array('name' => '无', 'parent_id' => 0);
                $value['double_parent_id'] = $parent['parent_id'];
                $value['has_child'] = $this->privilege->get_var(array('parent_id' => $value['id']), 'COUNT(*)');
                $value['parent_name'] = __($parent['name']);
                $value['cn_name'] = __($value['name']);
                $privilege_list[$key] = $value;
            }
        }
        unset($privilege_list_temp);
        $this->template->assign('current_id', $id);
        $this->template->assign('privilege_list', $privilege_list);
        $this->template->view('user_privilege_list');
    }

    public function action_edit()
    {
        $id = intval($this->request->param('id'));
        $privilege = $this->privilege->get_row(array('id' => $id));
        if (!count($privilege) > 0)
        {
            Message::set(Message::ERROR, "\"$privilege[name]\"" . '--该权限不存在');
            $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'list')));
        }

        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['module'] = Arr::get($_POST, 'module', '');
            $data['parent_id'] = Arr::get($_POST, 'parent_id', '');
            $data['controller'] = Arr::get($_POST, 'controller', '');
            $data['action'] = Arr::get($_POST, 'action', '');
            $data['data'] = Arr::get($_POST, 'data', '');
            $data['display'] = Arr::get($_POST, 'display', '');
            $data['is_url'] = Arr::get($_POST, 'is_url', '');
            $data['url'] = Arr::get($_POST, 'url', '');
            $data['position'] = intval(Arr::get($_POST, 'position', 0));

            if (!$this->privilege->validate($data))
            {
                Message::error(__('privilege validate error'));
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'add')));
            }

            if ($this->privilege->is_exist(array('name' => $data['name']), $id, 'parent_id=' . $data['parent_id']))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--权限名称已经存在');
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'edit', 'id' => $id)));
            }


            if ($this->privilege->update($data, array('id' => $id)))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--权限更新成功');
                $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'edit', 'id' => $id)));
            }
        }

        //获取一级的权限
        $privilege_option = $this->privilege->get_privilege_list(array('parent_id' => 0));
        $this->template->assign('privilege_option', $privilege_option);
        $this->template->assign('data', $privilege);
        $this->template->view('user_privilege_edit');
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));
        $privilege = $this->privilege->get_row(array('id' => $id), 'name,parent_id');
        if (!count($privilege) > 0)
        {
            Message::set(Message::ERROR, "\"$privilege[name]\"" . '--该权限不存在');
            $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'list')));
        }
        $has_child = $this->privilege->get_var(array('parent_id' => $id), 'COUNT(*)');
        if (!empty($has_child))
        {
            Message::set(Message::ERROR, "\"$privilege[name]\"" . '--该权限下有子分类，请先删除子权限');
            $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'list', 'id' => $privilege['parent_id'])));
        }

        if (!$this->privilege->delete(array('id' => $id)))
        {
            Message::set(Message::SUCCESS, "\"$privilege[name]\"" . '--权限删除失败');
            $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'list', 'id' => $privilege['parent_id'])));
        }
        Message::set(Message::SUCCESS, "\"$privilege[name]\"" . '--权限删除成功');
        $this->request->redirect(Route::get('admin/privilege')->uri(array('action' => 'list', 'id' => $privilege['parent_id'])));
    }

    //生成权限列表的缓存文件
    public function action_createfile()
    {
        $privilege_list = $this->privilege->get_privilege_list();
        $file_path = SYSTEM_PRIVILEGE_CACHE_PATH . 'privilege.php';
        if (Cache::instance()->write($file_path, Arr::setkey('id', $privilege_list)))
        {
            Message::success('写入成功');
            $this->request->redirect(URL::site('admin/privilege/list'));
        }
    }

}